<?php
namespace Home\Controller;

use Think\Controller;

header("Content-Type: text/html;charset=utf-8");

class TeacherController extends HomeController
{

    public function index()
    {
        $this->display('index');
    }

    public function account()
    {
        if (IS_POST) {
            $rules = array(
                array('phone', 'checkPhone', '手机号码格式不正确', 2, 'function'),
                array('phone', 'number', '手机号码格式不正确', 2),
                array('address', '/^[\x{4e00}-\x{9fa5}]+$/u', 2, '所在地必须填写中文名'),
            );
            $User = M('user_teacher');
            if (!$User->validate($rules)->create()) {
                exit($User->getError());
            } else {
                $result = $User->where('user_id=' . session('id'))->save(I('post.'));
                if ($result) {
                    $this->success('修改成功', '/Bailey/index.php/Home/Teacher/account', 1);
                } else {
                    $this->success('没有更改内容', '/Bailey/index.php/Home/Teacher/account', 1);
                }

            }
        } else {
            $find = array('user_id' => session('id'));
            $data['teacher'] = M('user_teacher')->join('RIGHT JOIN user ON user_teacher.user_id = user.id')->where($find)->find();
            $this->assign($data);
            $this->display('account');
        }
    }

    /*
     * 老师中心
     */
    public function center()
    {
        $teacher = M('user_teacher');
        $teacher_date = M('user_date');
        $user = M('user');
        $user_id = session('id');
        if(!IS_POST){
            $data = $teacher->where(array('user_id' => $user_id))->find();
            if(isset($data['grade'])){
                $data['grade'] = explode(",", $data['grade']);
            }
            if(isset($data['lesson'])){
                $data['lesson'] = explode(",", $data['lesson']);
            }
            $user = $user->where(array('id' => $user_id))->find();
            $data['status'] = $user['status'];
            $date = $teacher_date->where(array('user_id' => $user_id))->select();
            $school = include './Application/Pub/Dictionaries/school.php';

            $this->assign('school',$school);
            $this->assign('data',$data);
            $this->assign('date', $date);
        }else{
            /*$arr = I('post.');
            $arr['grade'] = implode(",", $arr['grade']);
            $arr['lesson'] = implode(",", $arr['lesson']);
            $teacher->where(array('user_id' => $user_id))->save($arr);
            if($teacher){
                $this->redirect('/Home/Teacher/center');
            }*/
        }
        $this->display('center');
    }

    /*
     * 负责ajax的请求
     */
    public function centerjs()
    {
        $teacher = M('user_teacher');
        $teacher_date = M('user_date');
        $user_id = session('id');
        if(!IS_POST){
            /*$data = $teacher->where(array('user_id' => $user_id))->find();
            if(isset($data['grade'])){
                $data['grade'] = explode(",", $data['grade']);
            }
            if(isset($data['lesson'])){
                $data['lesson'] = explode(",", $data['lesson']);
            }
            $date = $teacher_date->where(array('user_id' => $user_id))->select();
            $school = include './Application/Pub/Dictionaries/school.php';

            $this->assign('school',$school);
            $this->assign('data',$data);
            $this->assign('date', $date);*/
        }else{
            $arr = I('post.');
            $arr['grade'] = implode(",", $arr['grade']);
            $arr['lesson'] = implode(",", $arr['lesson']);
            $arr['is_full'] = (int)(I('post.is_full'));
            $teacher->where(array('user_id' => $user_id))->save($arr);
            if($teacher){
                $data['error'] = 0;
                $data['msg'] = "保存成功";
                //$this->redirect('/Home/Teacher/center');
            }else{
                $data['error'] = 1;
                $data['mgs'] = "保存失败";
            }
            echo json_encode($data);
        }
    }
    /*
     * 设置课程闲忙规则
     */
    public function setting()
    {
        $teacher_date = M('user_date');
        $time = I('get.time');
        $id = I('get.id');
        $status = I('get.status') == 0 ? 1:0;
        $result = array(
            'flag' => '0',
            'content' => '设置失败'
        );
        if($time && $id && $status>=0){
            $data[$time] = $status;
            $data['id'] = $id;
            $flag = $teacher_date->save($data);
            if($flag){
                $result = array(
                    'flag' => '1',
                    'content' => '设置成功'
                );
            }            
        }
        $this->ajaxReturn($result);        
    }
    
    
    /*
     * 编辑教师信息
     */
    public function edit()
    {
        $teacher = M('user_teacher');
        $user_id = session('id');
        
        if(IS_POST){
            $info['relname'] = I('post.relname');
            $info['school'] = I('post.school');
            $info['college'] = I('post.college');
            $info['year'] = I('post.year');
            $info['number'] = I('post.number');
            $info['teachage'] = I('post.teachage');
            $info['score'] = I('post.score');
            $info['description'] = I('post.description');
            $info['tutor_experience'] = I('post.tutor_experience');
            $info['person_experience'] = I('post.person_experience');
            $lesson = I('post.lesson');
            $grade = I('post.grade');
            $info['lesson'] = implode(",", $lesson);
            $info['grade'] = implode(",", $grade);
            $teacher->where(array('user_id' => $user_id))->save($info);
            $this->success('操作完成','/Teacher/center',3);       
        }        
        
        $data = $teacher->where(array('user_id' => $user_id))->find();
        if(isset($data['grade'])){
            $data['grade'] = explode(",", $data['grade']);
        }
        if(isset($data['lesson'])){
            $data['lesson'] = explode(",", $data['lesson']);
        }
        $this->assign($data);
        $this->display('edit');
    }
    /*
     * 个人背景
     */
    public function personal(){
        $this->display('personal');
    }

    public function famedu(){
        $this->display('famedu');
    }
    /*
     * 头像上传
     */
    public function upload(){

        $result = array(
            'flag' => 0,
            'url' => ''
        );
        if(IS_POST){
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize   =     3145728 ;// 设置附件上传大小
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  =     './upload/'; // 设置附件上传根目录
            $upload->savePath  =     ''; // 设置附件上传（子）目录
            // 上传文件 
            $info   =   $upload->upload();
            $avatar = '';
            foreach($info as $k => $v){
                $avatar = $v['savepath'].$v['savename'];
            }
            $teacher = M('user_teacher');
            $data['user_id'] = session('id');
            $data['avatar'] = $avatar;
            $isHave = $teacher->where('user_id',session('id'))->select();
            if($isHave){
                if($avatar!=""){
                    unset($data['user_id']);
                    $map['user_id'] = session('id');
                    $teacher->where($map)->save($data);
                }
                //echo M('user_teacher')->getLastSql();die();
            }else{
                $teacher->add($data);
            }
            if(!$info) {// 上传错误提示错误信息
                $this->error($upload->getError());
            }else{// 上传成功
//                
                $url = U('/Home/Teacher/upload');
                echo "<script>alert('上传成功')</script>";
                echo "<script>window.location.href='$url'</script>";
            }
        }else{
            $url = M('user_teacher')-> where(array('user_id' => session('id')))-> find();
//            echo M('user_teacher')->getLastSql();
//            var_dump($url);
            $this->assign(array('avatar' => $url['avatar']));
            $this->display('upload');
        }
    }
    
    /*
     * 老师订单列表
     */
    public function teacherOrder()
    {
        $id = session('id');
        $where = " r.teacher_id = $id";
        
        //页码
        if(I('get.page')){
            $page = I('get.page');
        }else{
            $page = 1;
        }
        
        $model = M("order_form"); 
        
        $limit = 8;
        $start = ($page - 1) * $limit;  
        
        $data = $model->join('LEFT JOIN requirement AS r ON r.id =  order_form.requirement_id ')->where($where)
            ->order('order_form.id DESC')->limit($start,$limit)
            ->field('order_form.status AS order_status, order_form.date,order_form.coursenum,order_form.order_sn, r.address,r.name')->select();
        //echo $model->getLastSql();die();
        /*$total = $model->join('LEFT JOIN requirement AS r ON r.id =  order_form.requirement_id ')->where($where)->count();
        
        $pagination = new \Pub\Controller\PaginationController();
        $pagination->total = $total;
        $pagination->page = $page;
        $pagination->limit = $limit;
        $pagination->url = U('Teacher/teacherOrder') . '?page={page}' . $url;
        $render = $pagination->render();

        $this->assign('rander', $render);*/
        $this->assign('lists', $data);
        $this->display('teacherOrder');
    }

    /*
     * 家长订单中心
     */
    public function courseorder()
    {
        $this->judgeParent();
        $id = session('id');
        if(I('get.type')=='all'){
            $where['requirement.user_id']=$id;
            //$where = " requirement.user_id = $id";
        }elseif(I('get.type')=='inpay'){
            $where['rf.status'] = 2;
            $where['requirement.user_id']=$id;
        }elseif(I('get.type')=='handing'){
            $where['rf.status'] = 3;
            $where['requirement.user_id']=$id;
        }elseif(I('get.type')=='dealing'){
            $where = " requirement.user_id = $id AND (rf.status=1 OR rf.status=4)";
            //$where['rf.status'] = 1;
            //$where['rf.status'] = 4;
            //$where['requirement.user_id']=$id;
        }elseif(I('get.type')=='end'){
            $where['rf.status'] = 5;
            $where['requirement.user_id']=$id;
        }else{
            $where['requirement.user_id']=$id;
        }
        $url = "";

        //页码
        if(I('get.page')){
            $page = I('get.page');
        }else{
            $page = 1;
        }


//        switch (I('get.status')) {
//            case 0:
//                $where .= " AND requirement.status = 0";
//                $url = "&status=0";
//                break;
//            case 1:
//                $where .= " AND requirement.status IN (1,2)";
//                $url = "&status=1";
//                break;
//            case 2:
//                $where .= " AND requirement.status = 3";
//                $url = "&status=2";
//                break;
//        }
        $limit = 50;
        $start = ($page - 1) * $limit;

        $model = M("requirement");
        //获取课程数据
        $data = $model->join('LEFT JOIN order_form rf ON rf.requirement_id = requirement.id')
                      ->join('LEFT JOIN user_teacher ut ON ut.user_id = requirement.teacher_id')
                      ->join('user u ON u.id = requirement.teacher_id')
                      ->where($where)->order('requirement.id desc')
                      ->limit($start,$limit)->field('requirement.*, ut.relname, ut.school, ut.sex,u.is_certified,rf.*')
                      ->select();
        foreach($data as $k=>$v){
            $map['grade'] = $v['grade'];
            $map['is_certified'] = $v['is_certified'];
            $result = M("course_price")->where($map)->find();
            $data[$k]['courseprice'] = $result['price'];
        }
        //$total = $model->join('LEFT JOIN order_form rf ON rf.requirement_id = requirement.id')->join('LEFT JOIN  user_teacher ut ON rf.user_id = ut.user_id')->where($where)->count();

        /*$pagination = new \Pub\Controller\PaginationController();
        $pagination->total = $total;
        $pagination->page = $page;
        $pagination->limit = $limit;
        $pagination->url = U('Teacher/parent') . '?page={page}' . $url;

        $render = $pagination->render();*/
        $this->assign('lists',$data);
        //$this->assign($status);
        $this->display('courseorder');
    }
    /*
     * 家长中心
     */
    public function parent()
    {
        $this->judgeParent();
        $id = session('id');
        $where = " requirement.user_id = $id"; 
        $url = "";
        
        //页码
        if(I('get.page')){
            $page = I('get.page');
        }else{
            $page = 1;
        }
        
        
//        switch (I('get.status')) {
//            case 0:
//                $where .= " AND requirement.status = 0";
//                $url = "&status=0";
//                break;
//            case 1:
//                $where .= " AND requirement.status IN (1,2)";
//                $url = "&status=1";
//                break;
//            case 2:
//                $where .= " AND requirement.status = 3";
//                $url = "&status=2";
//                break;
//        }        
        $limit = 8;
        $start = ($page - 1) * $limit;  
        
        $model = M("requirement"); 
        $data = $model->join('LEFT JOIN order_form rf ON rf.requirement_id = requirement.id')->join('LEFT JOIN  user_teacher ut ON rf.user_id = ut.user_id')->where($where)->order('requirement.id desc')->limit($start,$limit)->field('requirement.*, ut.relname, ut.school, ut.sex')->select();
        $total = $model->join('LEFT JOIN order_form rf ON rf.requirement_id = requirement.id')->join('LEFT JOIN  user_teacher ut ON rf.user_id = ut.user_id')->where($where)->count();
        
        $pagination = new \Pub\Controller\PaginationController();
        $pagination->total = $total;
        $pagination->page = $page;
        $pagination->limit = $limit;
        $pagination->url = U('Teacher/parent') . '?page={page}' . $url;
        
        $render = $pagination->render();
        $this->assign('data', $data);
        $this->assign($status);
        $this->display('parent');
    }
    
    /*
     * 家长信息修改
     */
    public function requirementEdit($id)
    {

        $id = (int)I('get.id');
        $user_id = session('id');
        
        $model = M('requirement');        
        $data = $this->judgeRequirementDel($id, $user_id, $model);
        if(IS_POST){
            $model->where(array('id' => $id))->save(I('post.'));
            $this->success('更新成功', 'Teacher/parent');
        }        
        $this->assign($data);
        $this->display('requirementEdit');
    }
    
    /*
     * 删除发布的信息
     */
    public function requirementDel($id)
    {
        $id = (int)I('get.id');
        $user_id = session('id');
        $model = M('requirement'); 
        
        $flag = $model->where(array('id' => $id, 'user_id' => $user_id, 'status' => 0))->delete();
        if($flag){
            $this->success('删除成功', 'Teacher/parent');
        }else{
            $this->success('删除失败', 'Teacher/parent');
        }
    }

    public function salary()
    {
        $id = session("id");
        $orderlog = M("orderlog");

        //$map['re.teacher_id'] = session('id');
        $map['orderlog.user_id'] = session('id');
        $data = $orderlog//->join('LEFT JOIN requirement re ON re.order_sn=orderlog.order_sn')
            ->where($map)->order('orderlog.id desc')
            ->field('order_sn as order_id,eventtime,remark,money,user_id')//,re.name
            ->select();
        /*老师信息
        foreach($data as $k=>$v){
            unset($map);
            $map['teacher_id'] = $v['user_id'];
            $rs =
            $data[$k]['name'] =
        }*/
        $user_teacher = M("user_teacher");
        $utmap['user_id'] = $id;
        $teacherinfo = $user_teacher->where($utmap)->find();
        $this->assign('total_salary',$teacherinfo['salary']);
        $this->assign('lists',$data);
        $this->display('salary');
    }

}